package com.xmb.wechat.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatContactBean_;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.bean.WechatMsgBean_;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.entity.WechatAppInfoBean;
import com.xmb.wechat.lsn.OnRefreshListener;
import com.xmb.wechat.lsn.SendMsgCenter;
import com.xmb.wechat.picasso.PicassoRoundByPercentTransform;
import com.xmb.wechat.picasso.PicassoSizeFitTransform;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.util.SpanStringUtils;
import com.xmb.wechat.util.WechatDateFormat;

import java.util.ArrayList;
import java.util.HashMap;

import io.objectbox.Box;
import io.objectbox.relation.ToMany;

public class ChatMsgAdapter extends BaseMultiItemQuickAdapter<WechatMsgBean, BaseViewHolder> {
    private static final int MSG_TYPE_NICK_NAME              = 776;
    private static final int MSG_TYPE_RED_PKG_SMALL_ICON     = 777;
    private static final int MSG_TYPE_ZHUAN_ZHANG_SMALL_ICON = 778;
    private static final int MSG_TYPE_ZHUAN_ZHANG_TV_TAG     = 779;
    private static final int MSG_TYPE_BUSINESS_CARDS_IV      = 780;

    private Activity                         ctx;
    private Typeface                         mTf;
    private boolean                          isHasBgPic;
    private boolean                          isRoomChat;
    private int                              mFontScaleLevel;
    private boolean                          mIsShowNickName;
    private WechatAppInfoBean                mMyInfoBean;
    private OnRefreshListener                mRefreshLsn;
    private WechatContactBean                mCurrentContactBean;
    private HashMap<Long, WechatContactBean> mHashMap;

    public ChatMsgAdapter(Activity ctx, boolean isRoomChat, WechatContactBean contactBean, GroupTalkBean groupTalkBean, OnRefreshListener refreshLsn) {
        super(new ArrayList<>());
        this.ctx = ctx;
        this.isRoomChat = isRoomChat;
        this.mRefreshLsn = refreshLsn;
        this.mMyInfoBean = WechatAppInfoBean.getWechat(ctx);
        //群聊成员
        if (isRoomChat) {
            ToMany<WechatContactBean> groupMembersList = groupTalkBean.getGroupMembersList();
            mHashMap = new HashMap<>();
            for (WechatContactBean wechatContactBean : groupMembersList) {
                mHashMap.put(wechatContactBean.getId(), wechatContactBean);
            }
            mIsShowNickName = groupTalkBean.isShowNickName();
        } else {
            mCurrentContactBean = contactBean;
        }

        //文字
        addItemType(WechatMsgBean.MSG_TYPE_STRING, R.layout.wechat_msg_item_string_byme);
        addItemType(WechatMsgBean.MSG_TYPE_STRING * -1, R.layout.wechat_msg_item_string_bycontact);
        //语音
        addItemType(WechatMsgBean.MSG_TYPE_VOICE, R.layout.wechat_msg_item_voice_byme);
        addItemType(WechatMsgBean.MSG_TYPE_VOICE * -1, R.layout.wechat_msg_item_voice_bycontact);
        //图片
        addItemType(WechatMsgBean.MSG_TYPE_IMG, R.layout.wechat_msg_item_img_byme);
        addItemType(WechatMsgBean.MSG_TYPE_IMG * -1, R.layout.wechat_msg_item_img_bycontact);
        //时间
        addItemType(WechatMsgBean.MSG_TYPE_TIME, R.layout.wechat_msg_item_time);
        addItemType(WechatMsgBean.MSG_TYPE_TIME * -1, R.layout.wechat_msg_item_time);
        //视频通话
        addItemType(WechatMsgBean.MSG_TYPE_VIDEO_CALL, R.layout.wechat_msg_item_video_call_byme);
        addItemType(WechatMsgBean.MSG_TYPE_VIDEO_CALL * -1, R.layout.wechat_msg_item_video_call_bycontact);
        //语音通话
        addItemType(WechatMsgBean.MSG_TYPE_VOICE_CALL, R.layout.wechat_msg_item_voice_call_byme);
        addItemType(WechatMsgBean.MSG_TYPE_VOICE_CALL * -1, R.layout.wechat_msg_item_voice_call_bycontact);
        //红包
        addItemType(WechatMsgBean.MSG_TYPE_RED_PKG, R.layout.wechat_msg_item_redpkg_byme);
        addItemType(WechatMsgBean.MSG_TYPE_RED_PKG * -1, R.layout.wechat_msg_item_redpkg_bycontact);
        //系统消息
        addItemType(WechatMsgBean.MSG_TYPE_SYS_TIP, R.layout.wechat_msg_item_systip);
        addItemType(WechatMsgBean.MSG_TYPE_SYS_TIP * -1, R.layout.wechat_msg_item_systip);
        //转账
        addItemType(WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG, R.layout.wechat_msg_item_zhuanzhang_byme);
        addItemType(WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG * -1, R.layout.wechat_msg_item_zhuanzhang_bycontact);
        //红包接收
        addItemType(WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE, R.layout.wechat_msg_item_redpkg_receive);
        addItemType(WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE * -1, R.layout.wechat_msg_item_redpkg_receive);
        //名片
        addItemType(WechatMsgBean.MSG_TYPE_BUINESS_CARDS, R.layout.wechat_msg_item_business_cards_byme);
        addItemType(WechatMsgBean.MSG_TYPE_BUINESS_CARDS * -1, R.layout.wechat_msg_item_business_cards_bycontact);

        AssetManager mgr = ctx.getAssets();
        mTf = Typeface.createFromAsset(mgr, "fonts/WeChatLqNumber.ttf");
        mFontScaleLevel = SPUtils.getInstance().getInt(Constant.SP.SP_KEY_FONT_SCALE_LEVEL, Constant.SP.SP_DEFAULT_FONT_SCALE_LEVEL);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(BaseViewHolder helper, WechatMsgBean vo) {
        int itemViewType = helper.getItemViewType();
        switch (itemViewType) {
            case WechatMsgBean.MSG_TYPE_STRING://文字
            case WechatMsgBean.MSG_TYPE_STRING * -1://文字【对方发的】
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_STRING, helper.getView(R.id.tv_content), 16);
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                //显示内容
                helper.setText(R.id.tv_content, SpanStringUtils.getEmotionContent(ctx, helper.getView(R.id.tv_content), vo.getContent()));
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_VOICE://语音
            case WechatMsgBean.MSG_TYPE_VOICE * -1://语音【对方发的】
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_VOICE, helper.getView(R.id.tv_content), 14);
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                //显示语音消息
                buildVoiceByLength(vo, helper.getView(R.id.tv_content));
                //是否已读：
                if (!vo.isMySend())
                    helper.setVisible(R.id.iv_unread, !vo.isRead());
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_IMG://图片
            case WechatMsgBean.MSG_TYPE_IMG * -1://图片【对方发的】
                //调整大小
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                ImageView iv = helper.getView(R.id.iv_img);
                Picasso.get().load("file:" + vo.getContent()).transform(new PicassoSizeFitTransform(getScaleMaxSize()))
                        .transform(new PicassoRoundByPercentTransform(40))
                        .error(R.drawable.mis_default_error).into(iv);
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_TIME://时间
            case WechatMsgBean.MSG_TYPE_TIME * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_TIME, helper.getView(R.id.tv_content), 12);
                //显示内容
                helper.setText(R.id.tv_content, buildTimeString(vo, ctx));
                if (isHasBgPic) {
                    helper.setTextColor(R.id.tv_content, ctx.getResources().getColor(R.color.black));
                    helper.setBackgroundRes(R.id.tv_content, R.drawable.shape_light_gray_2);
                }
                break;
            case WechatMsgBean.MSG_TYPE_VIDEO_CALL://视频通话
            case WechatMsgBean.MSG_TYPE_VIDEO_CALL * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_VIDEO_CALL, helper.getView(R.id.tv_content), 16);
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                //显示内容
                helper.setText(R.id.tv_content, "聊天时长 " + vo.getContent());
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_VOICE_CALL://语音通话
            case WechatMsgBean.MSG_TYPE_VOICE_CALL * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_VOICE_CALL, helper.getView(R.id.tv_content), 16);
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                //显示内容
                helper.setText(R.id.tv_content, "聊天时长 " + vo.getContent());
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_RED_PKG://红包
            case WechatMsgBean.MSG_TYPE_RED_PKG * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_RED_PKG, helper.getView(R.id.tv_content), 14);
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_RED_PKG, helper.getView(R.id.tv_remark), 13);
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_RED_PKG, helper.getView(R.id.tv_tag), 10);
                adjustViewSize(1, WechatMsgBean.MSG_TYPE_RED_PKG, helper.getView(R.id.iv_bg), 0);
                adjustViewSize(1, MSG_TYPE_RED_PKG_SMALL_ICON, helper.getView(R.id.iv_small_icon), 0);
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                //显示内容
                helper.setText(R.id.tv_content, vo.getMoneyRemark());
                helper.setBackgroundRes(R.id.iv_small_icon, vo.isReceiveMoney() ? R.drawable.wechat_hongbao_opened : R.drawable.wechat_hongbao_intact_new);
                if (helper.getItemViewType() > 0) {//我发的：
                    //背景
                    helper.setBackgroundRes(R.id.iv_bg, vo.isReceiveMoney() ? R.drawable.wechat_redpkg_right_yilingqu : R.drawable.wechat_redpkg_right);
                    //内容
                    TextView tvRemark = helper.getView(R.id.tv_remark);
                    TextView tvTag = helper.getView(R.id.tv_tag);
                    if (vo.isReceiveMoney()) {
                        tvRemark.setText("已被领完");
                        tvRemark.setVisibility(View.VISIBLE);
                        tvTag.setTextColor(ctx.getResources().getColor(R.color.red_packet_));
                    } else {
                        tvRemark.setVisibility(View.GONE);
                        tvTag.setTextColor(ctx.getResources().getColor(R.color.red_packet));
                    }
                } else {//对方发的：
                    helper.setBackgroundRes(R.id.iv_bg, vo.isReceiveMoney() ? R.drawable.wechat_redpkg_left_yilingqu : R.drawable.wechat_redpkg_left);
                    TextView tvRemark = helper.getView(R.id.tv_remark);
                    TextView tvTag = helper.getView(R.id.tv_tag);
                    if (vo.isReceiveMoney()) {
                        tvRemark.setText("已领取");
                        tvRemark.setVisibility(View.VISIBLE);
                        tvTag.setTextColor(ctx.getResources().getColor(R.color.red_packet_));
                    } else {
                        tvRemark.setVisibility(View.GONE);
                        tvTag.setTextColor(ctx.getResources().getColor(R.color.red_packet));
                    }
                }
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE://红包领取
            case WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE, helper.getView(R.id.tv_money_remark), 14);
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE, helper.getView(R.id.tv_money_remark_tag), 14);
                adjustViewSize(1, WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE, helper.getView(R.id.iv_small_icon), 16);
                //显示内容
                helper.setText(R.id.tv_money_remark, vo.getMoneyRemark());
                if (vo.isReceiveFinish())
                    helper.setText(R.id.tv_money_remark1, "，你的红包已被领完");
                //有背景图片时修改文字背景
                if (isHasBgPic) {
                    helper.setTextColor(R.id.tv_money_remark, ctx.getResources().getColor(R.color.black));
                    helper.setTextColor(R.id.tv_money_remark1, ctx.getResources().getColor(R.color.black));
                    helper.setTextColor(R.id.tv_money_remark_tag, ctx.getResources().getColor(R.color.red_packet_has_bg));
                    helper.setBackgroundRes(R.id.ll_content, R.drawable.shape_light_gray_2);
                }
                break;
            case WechatMsgBean.MSG_TYPE_SYS_TIP://系统提示
            case WechatMsgBean.MSG_TYPE_SYS_TIP * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_SYS_TIP, helper.getView(R.id.tv_content), 13);
                //显示内容
                helper.setText(R.id.tv_content, vo.getContent());
                //有背景图片时修改文字背景
                if (isHasBgPic) {
                    helper.setTextColor(R.id.tv_content, ctx.getResources().getColor(R.color.black));
                    helper.setBackgroundRes(R.id.tv_content, R.drawable.shape_light_gray_2);
                }
                break;
            case WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG://转账
            case WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG, helper.getView(R.id.tv_content), 14);
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG, helper.getView(R.id.tv_remark), 13);
                adjustViewSize(0, MSG_TYPE_ZHUAN_ZHANG_TV_TAG, helper.getView(R.id.tv_tag), 10);
                adjustViewSize(1, WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG, helper.getView(R.id.iv_bg), 0);
                adjustViewSize(1, MSG_TYPE_ZHUAN_ZHANG_SMALL_ICON, helper.getView(R.id.iv_small_icon), 0);
                //显示内容
                TextView tvContent = helper.getView(R.id.tv_content);
                tvContent.setTypeface(mTf);//修改字体
                tvContent.setText("¥" + MoneyFormat.formatMoney(vo.getMoney()));
                helper.setText(R.id.tv_remark, vo.getMoneyRemark());
                helper.setBackgroundRes(R.id.iv_small_icon, vo.isReceiveMoney() ? R.drawable.ic_shouqian : R.drawable.ic_zhuanzhang);
                //修改背景
                if (helper.getItemViewType() > 0) {//右边布局
                    helper.setBackgroundRes(R.id.iv_bg, vo.isReceiveMoney() ? R.drawable.wechat_zhuanzhang_right_yilingqu : R.drawable.wechat_zhuanzhang_right);
                } else {//左边布局
                    helper.setBackgroundRes(R.id.iv_bg, vo.isReceiveMoney() ? R.drawable.wechat_zhuanzhang_left_yilingqu : R.drawable.wechat_zhuanzhang_left);
                }
                setAvatar(helper, vo.getMsgSenderID());
                break;
            case WechatMsgBean.MSG_TYPE_BUINESS_CARDS://名片
            case WechatMsgBean.MSG_TYPE_BUINESS_CARDS * -1:
                //调整大小
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_BUINESS_CARDS, helper.getView(R.id.tv_b_cards), 16);
                adjustViewSize(0, WechatMsgBean.MSG_TYPE_BUINESS_CARDS, helper.getView(R.id.tv_small), 10);
                adjustViewSize(1, WechatMsgBean.MSG_TYPE_BUINESS_CARDS, helper.getView(R.id.iv_bg), 0);
                adjustViewSize(1, MSG_TYPE_BUSINESS_CARDS_IV, helper.getView(R.id.iv_b_cards), 0);
                if (itemViewType < 0)
                    adjustViewSize(0, MSG_TYPE_NICK_NAME, helper.getView(R.id.tv_nick_name), 12);
                //显示内容
                long businessCardsId = vo.getBusinessCardsId();
                WechatContactBean unique = WechatStartApplication.getBoxStore(ctx).boxFor(WechatContactBean.class).query().equal(WechatContactBean_.id, businessCardsId).build().findUnique();
                if (unique != null) {
                    helper.setText(R.id.tv_b_cards, unique.getName());
                    WechatContactBean.setupAvatarIntoImageView(unique, helper.getView(R.id.iv_b_cards), 12);
                }
                setAvatar(helper, vo.getMsgSenderID());
                break;
        }
        helper.itemView.setOnLongClickListener(v -> {
            doItemLongClick(helper, vo);
            return true;
        });
    }

    //处理Item的长按点击事件：
    private void doItemLongClick(BaseViewHolder helper, WechatMsgBean vo) {
        int itemType = helper.getItemViewType();
        if (itemType == WechatMsgBean.MSG_TYPE_RED_PKG || itemType == WechatMsgBean.MSG_TYPE_RED_PKG * -1) {//红包类型：
            String[] items = vo.isReceiveMoney() ? new String[]{"删除"} : new String[]{isRoomChat ? "标记已领完" : "领取红包", "删除"};
            new AlertDialog.Builder(ctx).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            if (vo.isReceiveMoney()) {
                                deleteMsg(vo);//删除
                            } else {//领取红包：
                                if (isRoomChat) {
                                    vo.setReceiveMoney(true);
                                    WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).put(vo);
                                    notifyDataSetChanged();
                                } else {
                                    SendMsgCenter.confirmReceiveSingleRedpkg(new SendMsgCenter.SendMsgListener() {
                                        @Override
                                        public void onSendSuc() {
                                            ToastUtils.showShort("已领取红包");
                                            mRefreshLsn.refresh();
                                        }

                                        @Override
                                        public void onSendError(String errorMsg) {
                                        }
                                    }, vo, mCurrentContactBean);
                                }
                            }
                            break;
                        case 1:
                            deleteMsg(vo);
                            break;
                    }
                }
            }).setNegativeButton("取消", null).show();
        } else if (itemType == WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG || itemType == WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG * -1) {//转账类型：
            String[] items = vo.isReceiveMoney() ? new String[]{"删除"} : new String[]{"确认收款", "删除"};
            new AlertDialog.Builder(ctx).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0://确认操作
                            if (vo.isReceiveMoney()) {
                                deleteZhuanZhang(vo);//删除
                            } else {//确认收款：
                                SendMsgCenter.confirmReceiveZhuanZhang(new SendMsgCenter.SendMsgListener() {
                                    @Override
                                    public void onSendSuc() {
                                        ToastUtils.showShort("已确认收款");
                                        mRefreshLsn.refresh();
                                    }

                                    @Override
                                    public void onSendError(String errorMsg) {
                                    }
                                }, vo);
                            }
                            break;
                        case 1://删除
                            deleteZhuanZhang(vo);
                            break;
                    }
                }
            }).setNegativeButton("取消", null).show();
        } else if (itemType == WechatMsgBean.MSG_TYPE_VOICE * -1) {//对方发的语音：
            String[] items = vo.isRead() ? new String[]{"设置为未读（显示小红点）", "撤回消息", "删除"} :
                    new String[]{"设置为已读（去掉小红点）", "撤回消息", "删除"};
            new AlertDialog.Builder(ctx).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            vo.setRead(!vo.isRead());
                            WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).put(vo);
                            ChatMsgAdapter.this.notifyDataSetChanged();
                            break;
                        case 1:
                            backMsg(vo);
                            break;
                        case 2:
                            deleteMsg(vo);
                            break;
                    }
                }
            }).setNegativeButton("取消", null).show();
        } else {
            String[] items = new String[]{"撤回消息", "删除"};
            new AlertDialog.Builder(ctx).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            backMsg(vo);
                            break;
                        case 1:
                            deleteMsg(vo);
                            break;
                    }
                }
            }).setNegativeButton("取消", null).show();
        }
    }

    //删除转账信息
    private void deleteZhuanZhang(WechatMsgBean vo) {
        Box<WechatMsgBean> msgBox = WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class);
        if (vo.getPid() != 0) {
            WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).remove(vo.getPid());
        } else {
            try {
                WechatMsgBean sonMsg = msgBox.query().equal(WechatMsgBean_.pid, vo.getId()).build().find().get(0);
                msgBox.remove(sonMsg);
            } catch (Exception e) {
                //do nothing
            }
        }
        WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).remove(vo);
        mRefreshLsn.refresh();
    }

    //撤回消息：
    private void backMsg(WechatMsgBean vo) {
        vo.setMsgType(WechatMsgBean.MSG_TYPE_SYS_TIP);
        WechatContactBean arg;
        if (isRoomChat) {
            arg = mHashMap.get(vo.getMsgSenderID());
        } else {
            arg = mCurrentContactBean;
        }
        if (arg == null)
            return;
        String content = (vo.isMySend() ? "你" : arg.getName()) + "撤回了一条消息";
        vo.setContent(content);
        WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).put(vo);
        ChatMsgAdapter.this.notifyDataSetChanged();
    }

    //删除消息：
    private void deleteMsg(WechatMsgBean vo) {
        WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).remove(vo);
        ChatMsgAdapter.this.getData().remove(vo);
        ChatMsgAdapter.this.notifyDataSetChanged();
    }

    //调节TextView的文字大小及View的大小
    private void adjustViewSize(int viewType, int sizeType, View view, int defaultSpValue) {
        if (mFontScaleLevel > 7)//角标越界
            return;
        if (viewType == 0) {//TextView
            TextView tv = (TextView) view;
            float value = 15;
            switch (sizeType) {
                case MSG_TYPE_NICK_NAME://昵称
                    value = Constant.mChatNickNameScale[mFontScaleLevel] * defaultSpValue;
                    break;
                case WechatMsgBean.MSG_TYPE_STRING:
                case WechatMsgBean.MSG_TYPE_SYS_TIP:
                case WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE:
                case WechatMsgBean.MSG_TYPE_BUINESS_CARDS:
                    value = Constant.mChatCommonTvScale[mFontScaleLevel] * defaultSpValue;
                    break;
                case WechatMsgBean.MSG_TYPE_VOICE:
                    value = Constant.mChatVoiceTvScale[mFontScaleLevel] * defaultSpValue;
                    break;
                case WechatMsgBean.MSG_TYPE_TIME:
                    value = Constant.mChatTimeTvScale[mFontScaleLevel] * defaultSpValue;
                    break;
                case WechatMsgBean.MSG_TYPE_VIDEO_CALL:
                case WechatMsgBean.MSG_TYPE_VOICE_CALL:
                    value = Constant.mChatCallTvScale[mFontScaleLevel] * defaultSpValue;
                    break;
                case WechatMsgBean.MSG_TYPE_RED_PKG:
                case WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG:
                    value = Constant.mRedPacketTvScale[mFontScaleLevel] * defaultSpValue;
                    break;
                case MSG_TYPE_ZHUAN_ZHANG_TV_TAG:
                    value = Constant.mRedPacketTvScale[mFontScaleLevel] * defaultSpValue;
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tv.getLayoutParams();
                    layoutParams.setMargins(0, 0, 0, getMarginBottomValue(mFontScaleLevel));
                    tv.setLayoutParams(layoutParams);
                    break;
            }
            tv.setTextSize(value);
        } else {//ImageView
            float width;
            float height;
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            switch (sizeType) {
                case WechatMsgBean.MSG_TYPE_RED_PKG_RECEIVE://接收红包
                    width = SizeUtils.dp2px(defaultSpValue) * Constant.mRedPacketReceivedIvWidth[mFontScaleLevel];
                    layoutParams.width = Float.valueOf(width).intValue();
                    layoutParams.height = Float.valueOf(width * 1.2f).intValue();
                    break;
                case WechatMsgBean.MSG_TYPE_ZHUAN_ZHANG://转账
                    width = Constant.mRedPacketWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth();
                    height = width / Constant.mZhuanZhangDimensionRatio[mFontScaleLevel];
                    layoutParams.width = Float.valueOf(width).intValue();
                    layoutParams.height = Float.valueOf(height).intValue();
                    break;
                case MSG_TYPE_ZHUAN_ZHANG_SMALL_ICON://转账中的转账图片
                    width = Constant.mZhuanZhangInnerIvWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth();
                    layoutParams.width = Float.valueOf(width).intValue();
                    break;
                case WechatMsgBean.MSG_TYPE_RED_PKG://红包
                case WechatMsgBean.MSG_TYPE_BUINESS_CARDS://名片
                    width = Constant.mRedPacketWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth();
                    height = width / Constant.mRedPacketDimensionRatio[mFontScaleLevel];
                    layoutParams.width = Float.valueOf(width).intValue();
                    layoutParams.height = Float.valueOf(height).intValue();
                    break;
                case MSG_TYPE_RED_PKG_SMALL_ICON://红包中的红包图片
                    width = Constant.mRedPacketInnerIvWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth();
                    layoutParams.width = Float.valueOf(width).intValue();
                    break;
                case MSG_TYPE_BUSINESS_CARDS_IV://名片中的名片头像
                    width = Constant.mChatIconWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth();
                    layoutParams.width = Float.valueOf(width).intValue();
                    layoutParams.height = Float.valueOf(width).intValue();
                    break;
            }
            view.setLayoutParams(layoutParams);
        }
    }

    //设置头像
    private void setAvatar(BaseViewHolder helper, long senderId) {
        ImageView ivAvatar = helper.getView(R.id.iv_avatar);
        if (helper.getItemViewType() >= 0) {
            Picasso.get().load("file://" + mMyInfoBean.getAvatar()).placeholder(R.drawable.ic_wx_default_avatar)
                    .error(R.drawable.ic_wx_default_avatar).transform(new PicassoRoundByPercentTransform(12)).into(ivAvatar);
        } else {
            if (isRoomChat) {
                WechatContactBean msgSender = mHashMap.get(senderId);
                if (msgSender == null)
                    return;
                //头像
                WechatContactBean.setupAvatarIntoImageView(msgSender, ivAvatar, 12);
                //昵称
                if (!mIsShowNickName)
                    return;
                TextView tvNickName = helper.getView(R.id.tv_nick_name);
                if (tvNickName == null)
                    return;
                tvNickName.setVisibility(View.VISIBLE);
                if (isHasBgPic)
                    tvNickName.setTextColor(Color.parseColor("#ffffff"));
                tvNickName.setText(TextUtils.isEmpty(msgSender.getName()) ? "" : msgSender.getName());
            } else {
                WechatContactBean.setupAvatarIntoImageView(mCurrentContactBean, ivAvatar, 12);
            }
        }
        ViewGroup.LayoutParams layoutParams = ivAvatar.getLayoutParams();
        int result = Float.valueOf(Constant.mChatIconWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth()).intValue();
        layoutParams.width = result;
        layoutParams.height = result;
        ivAvatar.setLayoutParams(layoutParams);
    }

    //动态调节语音View的宽度
    @SuppressLint("SetTextI18n")
    private void buildVoiceByLength(WechatMsgBean vo, TextView tv) {
        int second = Integer.parseInt(vo.getContent());
        int width;
        if (second <= 2) {
            width = ConvertUtils.dp2px(68);
        } else if (second <= 10) {
            width = ConvertUtils.dp2px((60 + (second - 2) * 10));
        } else if (second < 20) {
            width = ConvertUtils.dp2px(137);
        } else if (second < 30) {
            width = ConvertUtils.dp2px(145);
        } else if (second < 40) {
            width = ConvertUtils.dp2px(153);
        } else if (second < 50) {
            width = ConvertUtils.dp2px(161);
        } else if (second < 60) {
            width = ConvertUtils.dp2px(170);
        } else {
            width = ConvertUtils.dp2px(184);
        }
        tv.setText(vo.getContent() + "''");
        tv.setWidth(width);
    }

    /**
     * 获取图片的缩放后的显示宽度
     *
     * @return
     */
    private int getScaleMaxSize() {
        float result = Constant.mChatPicWithScale[mFontScaleLevel] * ScreenUtils.getScreenWidth() * 0.372f;
        return Float.valueOf(result).intValue();
    }

    //创建时间消息
    private String buildTimeString(WechatMsgBean vo, Context context) {
        return WechatDateFormat.formatInsideChat(vo.getMsgTime(), context);
    }

    /**
     * 是否设置了背景图
     *
     * @param isHasBgPic 是否存在
     */
    public void setIsHasBgPic(boolean isHasBgPic) {
        this.isHasBgPic = isHasBgPic;
    }

    /**
     * 获取转账底部TextView距底部的距离
     *
     * @param fontScaleLevel
     * @return
     */
    private int getMarginBottomValue(int fontScaleLevel) {
        switch (fontScaleLevel) {
            case 0:
            case 1:
                return SizeUtils.dp2px(5);
            case 2:
            case 3:
                return SizeUtils.dp2px(4);
            case 4:
            case 5:
                return SizeUtils.dp2px(3);
            case 6:
            case 7:
                return SizeUtils.dp2px(2);
        }
        return SizeUtils.dp2px(5);
    }
}